//
//  GGTitleScrollView.m
//  oneDemo
//
//  Created by engfei on 2016/11/18.
//  Copyright © 2016年 GaiGai. All rights reserved.
//

#import "GGTitleScrollView.h"
#import "UIView+GGLayout.h"
//#import "GGTitleModel.h"
//#import "UIView+Frame.h"

// #define buttonW 65
#define kOffset 0

#define kTitleMargin 30



@interface GGTitleScrollView ()
{
    UIColor *_norColor;
    UIColor *_selColor;
    UIColor *_undColor;
    CGFloat _underH;
    CGFloat _textFont;
    BOOL    _isShow;
//    CGFloat _gg_x; // 位置矫正
    NSArray *_gg_Models;
    UIButton *lastButton;
    
    
    
    CGFloat WidthOfTheButton;
}


@property (nonatomic, weak) UIButton *firstButton;
@property (nonatomic, weak) UIButton *selectButton;
@property (nonatomic, weak) UIButton *previousButton;
@property (nonatomic, weak) UIView *underlineView;

@property (nonatomic) CGSize kContentSize;

@end


@implementation GGTitleScrollView


- (instancetype)init{
    if (self = [super init]) {
        _norColor = [UIColor grayColor];
        _selColor = [UIColor blackColor];
        _undColor = [UIColor greenColor];
        _underH = 2.0f;
        _textFont = 13.0f;
        _isShow = YES;
        self.showsHorizontalScrollIndicator = NO;
        self.showsVerticalScrollIndicator = NO;
//        self.delegate = self;
    }
    return self;
}
-(void)awakeFromNib{
    [super awakeFromNib];
    _norColor = [UIColor blackColor];
    _selColor = APPMianColor;
    _undColor = [UIColor greenColor];
    _underH = 2.0f;
    _textFont = 13.0f;
    _isShow = YES;
    self.showsHorizontalScrollIndicator = NO;
    self.showsVerticalScrollIndicator = NO;
}

- (void)setSubviewWithTtiles:(NSArray *)titles{
    
    NSInteger num = titles.count;
    self.showsVerticalScrollIndicator = YES;
    self.showsHorizontalScrollIndicator = YES;
    self.alwaysBounceVertical = NO;
    _gg_Models = titles;

    
    WidthOfTheButton = 100;

     [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    self.previousButton = nil;
    CGFloat KscreenWidth = self.frame.size.width;
    // 滚动范围
    CGFloat scrollW = WidthOfTheButton*num +(num+1)*kTitleMargin;
    if (KscreenWidth>scrollW) {
        
        WidthOfTheButton =(KscreenWidth-((num+1)*kTitleMargin))/num;
        
    }
    
    for (int i = 0; i < num; i ++) {
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
       
        if (0 == i) {
            self.firstButton = btn;
        }
        
        [self addSubview:btn];
        [btn setTitle:[titles objectAtIndex:i] forState:UIControlStateNormal];
        [btn setTitleColor:_norColor forState:UIControlStateNormal];
        [btn setTitleColor:_selColor forState:UIControlStateSelected];
         btn.tag = i+100;
        [btn sizeToFit];
        [btn addTarget:self action:@selector(clickBnt:) forControlEvents:UIControlEventTouchUpInside];
        btn.titleLabel.font = [UIFont systemFontOfSize:_textFont];
        CGFloat btnWidth = [YYJ_PublicClass user_getSize:[UIFont systemFontOfSize:_textFont] With:MAXFLOAT Hight:self.gg_height String:btn.currentTitle].width;
        if (self.itemWidth) {
            btnWidth = self.itemWidth;
        }
        if (self.itemMargin == 999) {
            self.itemMargin = kTitleMargin;
        }
        btn.frame = CGRectMake(CGRectGetMaxX(self.previousButton.frame) + (i==0?0:self.itemMargin), 0,btnWidth, self.gg_height);
        self.previousButton = btn;
        
    }
    
    UIButton *btn = self.subviews[self.subviews.count - 1];
    [self setContentSize:CGSizeMake(CGRectGetMaxX(btn.frame) + self.itemMargin, CGRectGetHeight(self.frame))];
 
    [self setContentOffset:CGPointMake(0, -kOffset) animated:YES];


    UIView *underlineView = [[UIView alloc] init];
    underlineView.hidden = !_isShow;
    self.underlineView = underlineView;
    underlineView.backgroundColor = _undColor;
    underlineView.gg_height = _underH;
    underlineView.gg_y = self.gg_height - underlineView.gg_height - kOffset;
    if (self.isShowUnderline) {
        self.underlineView.hidden = NO;
        [self addSubview:underlineView];
    }
    [self clickBnt:self.firstButton];
}

- (void)setSubviewWithTtilesJudge:(NSArray *)titles : (BOOL)judge{
    
    NSInteger num = titles.count;
    self.showsVerticalScrollIndicator = YES;
    self.showsHorizontalScrollIndicator = YES;
    self.alwaysBounceVertical = NO;
    _gg_Models = titles;
    
    
    WidthOfTheButton = 352 - 20;
    
    
    CGFloat KscreenWidth = self.frame.size.width;
    // 滚动范围
    CGFloat scrollW = WidthOfTheButton*num +(num+1)*kTitleMargin;
    if (KscreenWidth>scrollW) {
        
        WidthOfTheButton =(KscreenWidth-((num+1)*kTitleMargin))/num;
        
    }
    
    for (int i = 0; i < num; i ++) {
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
      
        if (0 == i) {
            self.firstButton = btn;
        }
        
        [self addSubview:btn];
        [btn setTitle:[titles objectAtIndex:i] forState:UIControlStateNormal];
        [btn setTitleColor:_norColor forState:UIControlStateNormal];
        [btn setTitleColor:_selColor forState:UIControlStateSelected];
        btn.tag = i+100;
        [btn sizeToFit];
        [btn addTarget:self action:@selector(clickBnt:) forControlEvents:UIControlEventTouchUpInside];
        btn.titleLabel.font = [UIFont systemFontOfSize:_textFont];
        
        btn.frame = CGRectMake(CGRectGetMaxX(self.previousButton.frame) + kTitleMargin, 0,WidthOfTheButton, self.gg_height);
        
        //        btn.gg_height = self.gg_height;
        //        btn.gg_x = CGRectGetMaxX(self.previousButton.frame) + kTitleMargin;
        //        btn.gg_y = 0;
        self.previousButton = btn;
        
    }
    
    UIButton *btn = self.subviews[self.subviews.count - 1];
    [self setContentSize:CGSizeMake(CGRectGetMaxX(btn.frame) + kTitleMargin, 0)];
    
    [self setContentOffset:CGPointMake(0, -kOffset) animated:YES];
    
    UIView *underlineView = [[UIView alloc] init];
    underlineView.hidden = !_isShow;
    self.underlineView = underlineView;
    underlineView.backgroundColor = _undColor;
    underlineView.gg_height = _underH;
    underlineView.gg_y = self.gg_height - underlineView.gg_height - kOffset;
    
    [self addSubview:underlineView];
    
    if (judge) {
        [self clickBnt:self.firstButton];
    }
    
    
}


- (void)clickBnt:(UIButton *)sender{
    
    if (sender.selected) {
        return;
    }
    [sender.titleLabel sizeToFit];
    
    
    self.selectButton.selected = NO;
    sender.selected = YES;
    self.selectButton = sender;
    
    
    [UIView animateWithDuration:0.15 animations:^{
        self.underlineView.gg_width = sender.titleLabel.gg_width;
        self.underlineView.gg_centerx = sender.gg_centerx;
    } completion:^(BOOL finished) {
        
    }];
    
    CGFloat KscreenWidth = self.frame.size.width;
    // 滚动范围
    CGFloat scrollW = self.contentSize.width;
//    [self scrollRectToVisible:sender.frame animated:YES];
    // 点击右侧文字向左滚动
    if (sender.gg_centerx > KscreenWidth * 0.5) {
        // 剩余的宽度大于屏幕的一半时
        if (scrollW - sender.gg_centerx > KscreenWidth * 0.5) {
            [self setContentOffset:CGPointMake(sender.gg_centerx -KscreenWidth/2, -kOffset) animated:YES];
        }else{
            [self setContentOffset:CGPointMake(scrollW - KscreenWidth, -kOffset) animated:YES];
        }
    }else{ // 点击左侧文字向右滚动
        [self setContentOffset:CGPointMake(0, -kOffset) animated:YES];
    }

    
    if ([_gg_delegate respondsToSelector:@selector(titleScrollView:index:title:)]) {
        self.kCurreIndex = (sender.tag-100);
        [_gg_delegate titleScrollView:self index:(sender.tag-100) title:sender.titleLabel.text];
    }
}

-(void)seletedIndex:(NSInteger)index{
    UIButton * sender = (UIButton *)[self viewWithTag:index+100];
    [self clickBnt:sender];
}

- (void)SetTheLocation:(int)index : (BOOL)refresh {
    UIButton * sender = (UIButton *)[self viewWithTag:index+100]; ;
    if (sender.selected) {
        return;
    }
    [sender.titleLabel sizeToFit];
    
    
    self.selectButton.selected = NO;
    sender.selected = YES;
    self.selectButton = sender;
    
    
    [UIView animateWithDuration:0.15 animations:^{
        self.underlineView.gg_width = sender.titleLabel.gg_width;
        self.underlineView.gg_centerx = sender.gg_centerx;
    } completion:^(BOOL finished) {
        
    }];
    
    CGFloat KscreenWidth = self.frame.size.width;
    // 滚动范围
    CGFloat scrollW = self.contentSize.width;
    // 点击右侧文字向左滚动
    if (sender.gg_centerx > KscreenWidth * 0.5) {
        // 剩余的宽度大于屏幕的一半时
        if (scrollW - sender.gg_centerx > KscreenWidth * 0.5) {
            [self setContentOffset:CGPointMake(sender.gg_centerx -KscreenWidth/2, -kOffset) animated:YES];
        }else{
            [self setContentOffset:CGPointMake(scrollW - KscreenWidth, -kOffset) animated:YES];
        }
    }else{ // 点击左侧文字向右滚动
        [self setContentOffset:CGPointMake(0, -kOffset) animated:YES];
    }
    
    if (refresh) {
        if ([_gg_delegate respondsToSelector:@selector(titleScrollView:index:title:)]) {
            [_gg_delegate titleScrollView:self index:(sender.tag-100) title:sender.titleLabel.text];
        }
    }
    
    
}




- (void)setNormalColor:(UIColor *)normalColor{
    _normalColor = normalColor;
    _norColor = normalColor;
}
- (void)setSelectColor:(UIColor *)selectColor{
    _selectColor = selectColor;
    _selColor = selectColor;
}

- (void)setUnderlineH:(CGFloat)underlineH{
    _underlineH = underlineH;
    _underH = underlineH;
}

- (void)setIsShowUnderline:(BOOL)isShowUnderline{
    _isShowUnderline = isShowUnderline;
    _isShow = isShowUnderline;
}

- (void)setTitleFont:(CGFloat)titleFont{
    _titleFont = titleFont;
    _textFont = titleFont;
}

- (void)setUnderlineColor:(UIColor *)underlineColor{
    _underlineColor = underlineColor;
    _undColor = underlineColor;
}


@end
